package com.ruoyi.project.survey.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.project.survey.domain.dto.QuestionDto;
import com.ruoyi.project.survey.domain.entity.Question;
import com.ruoyi.project.survey.domain.entity.UserQuestion;
import com.ruoyi.project.survey.domain.param.QuestionDetailQuery;
import com.ruoyi.project.survey.domain.param.UserQuestionAdd;
import com.ruoyi.project.survey.domain.param.UserQuestionQuery;
import com.ruoyi.project.survey.mapper.QuestionMapper;
import com.ruoyi.project.survey.mapper.UserQuestionMapper;
import com.ruoyi.project.survey.service.UserQuestionService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 做问卷表(UserQuestion)表服务实现类
 *
 * @author makejava
 * @since 2024-03-17 08:41:43
 */
@Service("userQuestionService")
@RequiredArgsConstructor
public class UserQuestionServiceImpl implements UserQuestionService {
    private final UserQuestionMapper userQuestionMapper;
    private final QuestionMapper questionMapper;


    @Override
    public List<QuestionDto> queryPage(UserQuestionQuery query) {
        List<Question> questionList= questionMapper.selectList(new LambdaQueryWrapper<Question>().orderByDesc(Question::getEdate));
        List<QuestionDto> list=new ArrayList<>();
        questionList.forEach(q->{
            QuestionDto dto=new QuestionDto();
            BeanUtils.copyProperties(q,dto);
            dto.setFlag(checkFlag(q.getSdate(),q.getEdate()));
            list.add(dto);
        });
        return list;
    }
    private boolean checkFlag(Date sdate,Date edate){
        //验证日期
        SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMdd");
        //开始日期小于等于当前日期  && 结束日期 大于等于当前日期
        String date=sdf.format(new Date());
        return sdf.format(sdate).compareTo(date)<=0 && sdf.format(edate).compareTo(date)>=0;
    }
    @Transactional
    @Override
    public AjaxResult save(UserQuestionAdd add) {
        //部门+姓名+问卷名称一样的只能做一次
        LambdaQueryWrapper<UserQuestion> wrapper=new LambdaQueryWrapper<>();
        wrapper.eq(UserQuestion::getDept,add.getDept());
        wrapper.eq(UserQuestion::getName,add.getName());
        wrapper.eq(UserQuestion::getQid,add.getQid());
        wrapper.last("limit 1");
        if(userQuestionMapper.selectOne(wrapper)!=null){
            return AjaxResult.error("亲，改问卷你已经做过了！");
        }
        //只能做有效的问卷 当前时间在 开始日期和结束日期之间的
        Question question= questionMapper.selectById(add.getQid());
        if(!checkFlag(question.getSdate(),question.getEdate())){
            return AjaxResult.error("亲，问卷未开始或已结束！");
        }

        UserQuestion uq=new UserQuestion();
        BeanUtils.copyProperties(add,uq);
        uq.setCtime(new Date());
        //答案 -- 问题
        StringBuffer buffer=new StringBuffer();
        for (int i = 0; i < add.getQuestions().size(); i++) {
            buffer.append((i+1)+"."+add.getQuestions().get(i));
            buffer.append("回答："+add.getAnswers().get(i));
            if(i<add.getQuestions().size()-1){
                buffer.append("\r\n");
            }
        }
        uq.setAnswer(buffer.toString());
        userQuestionMapper.insert(uq);
        //新增数量
        questionMapper.updateNum(add.getQid());
        return AjaxResult.success();
    }

    @Override
    public List<UserQuestion> queryDetail(QuestionDetailQuery query) {
        LambdaQueryWrapper<UserQuestion> wrapper=new LambdaQueryWrapper<>();
        wrapper.eq(UserQuestion::getQid,query.getQid());
        if(StringUtils.hasLength(query.getDept())){
            wrapper.like(UserQuestion::getDept,query.getDept());
        }
        wrapper.orderByDesc(UserQuestion::getCtime);
        List<UserQuestion> userQuestionList = userQuestionMapper.selectList(wrapper);
        return userQuestionList;
    }
}

